#include "syscalls.h"
#include <unistd.h>
#include <stdlib.h>

FILE _iob[OPEN_MAX] = {		// stdin, stdout, stderr
	{ 0, (char *) 0, (char *) 0, _READ, 0 },
	{ 0, (char *) 0, (char *) 0, _WRITE, 1 },
	{ 0, (char *) 0, (char *) 0, _WRITE | _UNBUF, 2 }
};

// _fillbuf: allocate and fill input buffer
int _fillbuf(FILE *fp)
{
	int bufsize;
	
	if((fp->flag & (_READ | _EOF | _ERR)) != _READ)
		return EOF;
	bufsize = (fp->flag & _UNBUF) ? 1 : BUFSIZ;
	if(fp->base == NULL)	// no buffer yet
		if((fp->base = (char *)malloc(bufsize)) == NULL)
			return EOF;	// can't get buffer
	fp->ptr = fp->base;
	fp->cnt = read(fp->fd, fp->ptr, bufsize);
	if(--fp->cnt < 0)
	{
		if(fp->cnt == -1)
			fp->flag |= _EOF;
		else
			fp->flag |= _ERR;
		fp->cnt = 0;
		return EOF;
	}
	return (unsigned char ) *fp->ptr++;
}
